﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.ApiWrites.Applocation.Command;
using RBAC.ResultCode;

namespace RBAC.ApiWrites.Controllers
{
    /// <summary>
    /// API控制器 写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ManagementController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<ManagementController> logger;

        public ManagementController(IMediator mediator,ILogger<ManagementController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 权限新增
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddPermission(AddPermissionCommand request)
        {
            try
            {
                logger.LogInformation("日志信息");

                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 权限删除
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeletePermission(DeletePermissionCommand request)
        {
            try
            {
                logger.LogInformation("权限删除日志信息");

                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 权限修改
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdatePermission(UpdatePermissionCommand request)
        {
            try
            {
                logger.LogInformation("权限修改日志信息");

                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 角色新增
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddRole(AddRoleCommand request)
        {
            try
            {
                logger.LogInformation("日志信息");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ApiResult<int>> DeleteRole(DeleteRoleCommand request)
        {
            try
            {
                logger.LogInformation("日志信息");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateRole(UpdateRoleCommand request)
        {
            try
            {
                logger.LogInformation("修改角色信息日志信息");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 新增用户信息
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddUser(AddUserCommand request)
        {
            try
            {
                logger.LogInformation("添加用户信息日志信息");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]  
        public async Task<ApiResult<int>> DeleteUser(DeleteUserCommand request, CancellationToken cancellationToken)
        {
            try
            {
                logger.LogInformation("角色删除日志信息");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 用户分配角色、功能
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddUserfunction(AddUserfunctionsCommand request, CancellationToken cancellationToken)
        {
            try
            {
                logger.LogInformation("用户分配角色、功能日志信息");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 修改用户角色信息
        /// </summary>
        /// <param name="request">请求</param>
        /// <returns>返回受影响行数</returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateUserRole(UpdateUserRoleCommand request)
        {
            try
            {
                logger.LogInformation(" 修改用户角色信息日志信息");
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("=>" + ex.Message);

                throw;
            }
        }
        }
}
